// course/pages/courseallevalute/courseallevalute.js
import { BASE_URL } from '../../../http/config';

Page({
  data: {
    courseId: 0,
    evaluateList: [],
    pageNum: 1,
    pageSize: 10,
    hasMore: true,
    loading: false
  },

  // 获取课程评价列表
  getCourseEvaluateList(isRefresh = false) {
    if (this.data.loading) return;
    if (!this.data.hasMore && !isRefresh) return;

    if (isRefresh) {
      this.setData({
        pageNum: 1,
        hasMore: true,
        evaluateList: []
      });
    }

    this.setData({ loading: true });

    wx.request({
      url: `${BASE_URL}/courseEvaluate/all`,
      method: 'GET',
      data: {
        courseid: this.data.courseId,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      },
      success: (res) => {
        if (res.data.code === 200) {
          const newList = res.data.data.list || [];
          const hasMore = newList.length >= this.data.pageSize;

          this.setData({
            evaluateList: isRefresh ? newList : this.data.evaluateList.concat(newList),
            hasMore: hasMore,
            pageNum: this.data.pageNum + 1
          });
        } else {
          wx.showToast({
            title: '获取评价失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ loading: false });
      }
    });
  },

  // 生命周期函数--监听页面加载
  onLoad(options) {
    const courseId = options.courseid || 0;
    this.setData({ courseId: courseId });
    this.getCourseEvaluateList(true); // 第一次加载调用刷新
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.getCourseEvaluateList(true);
    wx.stopPullDownRefresh();
  },

  // 上拉加载更多
  onReachBottom() {
    this.getCourseEvaluateList(false);
  }
});
